import countries from '@/assets/json/countries.json'
export const state = () => ({
  counter: 1,

  locale: 'en',
  locales: ['en', 'zh', 'tw', 'ko', 'ja', 'pt', 'es', 'de', 'fr', 'vi'],

  toolify_isLogin: '',
  userinfo: {},
  //用户的广告信息
  advertiser: null,
  //最大广告主
  top_advertiser: null,

  languagesOptions: [
    {
      value: 'en',
      eleVal: 'en',
      label: 'English',
    },
    {
      value: 'zh',
      eleVal: 'zh-CN',
      label: '简体中文',
    },
    {
      value: 'tw',
      eleVal: 'zh-TW',
      label: '繁體中文',
    },
    {
      value: 'ko',
      eleVal: 'ko',
      label: '한국어',
    },
    {
      value: 'ja',
      eleVal: 'ja',
      label: '日本語',
    },
    {
      value: 'pt',
      eleVal: 'pt',
      label: 'Português',
    },
    {
      value: 'es',
      eleVal: 'es',
      label: 'Español',
    },
    {
      value: 'de',
      eleVal: 'de',
      label: 'Deutsch',
    },
    {
      value: 'fr',
      eleVal: 'fr',
      label: 'Français',
    },
    {
      value: 'vi',
      eleVal: 'vi',
      label: 'Tiếng Việt',
    },
  ],

  countries: countries,

  //搜索栏选项
  searchTools: [],
  searchCategory: [],
  searchLoading: [],

  exposureTime: null,
})

export const mutations = {
  increment(state) {
    state.counter++
  },

  SET_LANG(state, locale) {
    let tIndex = state.languagesOptions.findIndex((item) => {
      return item.value === locale
    })
    if (tIndex >= 0) {
      this.$cookies.set('locale', locale, {
        maxAge: 60 * 60 * 24 * 365,
        path: '/',
      })
      state.locale = locale
    }
  },

  SET_ISLOGIN(state, isLogin) {
    this.$cookies.set('toolify_isLogin', isLogin, {
      maxAge: 60 * 60 * 24 * 90,
      path: '/',
    })
    state.toolify_isLogin = isLogin
  },
  SET_USERINFO(state, data) {
    this.$cookies.set('toolify_userinfo', data, {
      maxAge: 60 * 60 * 24 * 90,
      path: '/',
    })
    state.userinfo = data
  },
  SET_ADVERTISER(state, data) {
    this.$cookies.set('toolify_advertiser', data, {
      maxAge: 60 * 60 * 24 * 90,
      path: '/',
    })
    state.advertiser = data
  },
  SET_TOP_ADVERTISER(state, data) {
    this.$cookies.set('toolify_top_advertiser', data, {
      maxAge: 60 * 60 * 24 * 90,
      path: '/',
    })
    state.top_advertiser = data
  },

  SET_SearchLoading(state, data) {
    state.searchLoading = data
  },
  SET_SearchTools(state, data) {
    state.searchTools = data
    if (process.browser) {
      window.localStorage.setItem('toolify-searchTools', JSON.stringify(data))
    }
  },
  SET_SearchCategory(state, data) {
    state.searchCategory = data
    if (process.browser) {
      window.localStorage.setItem(
        'toolify-searchCategory',
        JSON.stringify(data)
      )
    }
  },

  //修改exposureTime以重新监听曝光dom
  SET_RetrackExposure(state, data) {
    state.exposureTime = data
  },
}

export const actions = {
  nuxtServerInit(store, { app: { $cookies } }) {
    const locale = $cookies.get('locale') ? $cookies.get('locale') : 'en'
    store.commit('SET_LANG', locale)

    const isLogin = $cookies.get('toolify_isLogin')
      ? $cookies.get('toolify_isLogin')
      : false
    store.commit('SET_ISLOGIN', isLogin)

    const userinfo = $cookies.get('toolify_userinfo')
      ? $cookies.get('toolify_userinfo')
      : {}
    store.commit('SET_USERINFO', userinfo)
  },

  exposure({}, data) {
    this.$axiosApi.postCa(data)
  },
}
